const app = getApp();

function pointInPoly(checkPoint, polygonPoints) { //判断不可达区域
    var counter = 0;
    var xinters;
    var p1, p2;
    var pointCount = polygonPoints.length;
    p1 = polygonPoints[0];

    for (var i = 1; i <= pointCount; i++) {
        p2 = polygonPoints[i % pointCount];
        if (
            checkPoint.x > Math.min(p1.x, p2.x) &&
            checkPoint.x <= Math.max(p1.x, p2.x)
        ) {
            if (checkPoint.y <= Math.max(p1.y, p2.y)) {
                if (p1.x != p2.x) {
                    xinters =
                        (checkPoint.x - p1.x) *
                        (p2.y - p1.y) /
                        (p2.x - p1.x) +
                        p1.y;
                    if (p1.y == p2.y || checkPoint.y <= xinters) {
                        counter++;
                    }
                }
            }
        }
        p1 = p2;
    }
    if (counter % 2 == 0) {
        return false;
    } else {
        return true;
    }
}

function point_status(asda, arrays) { //判断位置是否在不可达区域
    var status = false
    for (var e = 0; e < arrays.length; e++) {
        if (pointInPoly(asda, arrays[e])) {
            status = true;
            break
        }
    };
    return status;
}

function searchMax(arr) { //取数组中最多的数
    var maxCount = 0,
        maxItem = '',
        obj = {},
        arrb = [];
    for (let i = 0; i < arr.length; i++) {
        arrb.push(arr[i].minor)//设备的次 id
    }
    arrb.forEach(function(item) {
        obj[item] ? (obj[item].count += 1) : obj[item] = {
            count: 1
        }
        obj[item].count > maxCount && (maxCount = obj[item].count, maxItem = item)
    })

    for (let i = 0; i < arr.length; i++) {
        if (maxItem == arr[i].minor) {
            maxItem = arr[i]
        }
    }
    return {
        item: maxItem,
        count: maxCount
    }

}

function getAngle(p, m, c) { //获得指针中心和鼠标坐标连线，与y轴正半轴之间的夹角,p物体中心坐标，m鼠标坐标
    var that = this;
    var x = Math.abs(p.x - m.x);
    var y = Math.abs(p.y - m.y);
    var z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
    var cos = y / z;
    var radina = Math.acos(cos); //用反三角函数求弧度

    //将弧度转换成角度
    var angle = Math.floor(180 / (Math.PI / radina));

    if (m.x > p.x && m.y > p.y) { //鼠标在第四象限
        angle = 180 - angle;
    }

    if (m.x == p.x && m.y > p.y) { //鼠标在y轴负方向上
        angle = 180;
    }

    if (m.x > p.x && m.y == p.y) { //鼠标在x轴正方向上
        angle = 90;
    }

    if (m.x < p.x && m.y > p.y) { //鼠标在第三象限
        angle = 180 + angle;
    }

    if (m.x < p.x && m.y == p.y) { //鼠标在x轴负方向
        angle = 270;
    }

    if (m.x < p.x && m.y < p.y) { //鼠标在第二象限
        angle = 360 - angle;
    }

    return angle;
    // return angle + (parseInt(c / 360) * 360);
}

function pauxi(property) { //排序
    return function(obj1, obj2) {
        var value1 = obj1[property];
        var value2 = obj2[property];
        // return value1 - value2; // 升序
        return value2 - value1; // 降序
    }
}

function formats(data) { //过滤蓝牙卡，排序
    var newDatas = [];
    for (var i = 0; i < data.length; i++) {
        if (data[i].accuracy < 16) {
            newDatas.push(data[i]);
        }
    };
    newDatas = newDatas.sort(pauxi("accuracy"));
    return newDatas
}

function format(data) { //过滤蓝牙卡
    var newDatas = [];
    for (var i = 0; i < data.length; i++) {
        //uuid  设备广播的 uuid
        //major  设备的主 id
        //minor  设备的次 id
        //proximity  设备距离的枚举值
        //accuracy  设备的距离
        //rssi  表示设备的信号强度
        if (data[i].rssi !== 0 && data[i].accuracy > 0 && data[i].minor && data[i].rssi > -80 && data[i].rssi) {
            var newDataObj = {};
            newDataObj.autonum = data[i].minor;
            newDataObj.minor = data[i].minor;
            newDataObj.rssi = data[i].rssi;
            newDataObj.accuracy = data[i].accuracy;
            newDatas.push(newDataObj);
        }
    };
    newDatas = newDatas.sort(pauxi("rssi")); //按信号强度排序
    // newDatas = newDatas.sort(pauxi("accuracy"));
    return newDatas
}

function coordinate(k, s, p, f) { //坐标转换
    return {
        x: k.x + Math.floor(s * p * Math.sin(f * Math.PI / 180) * 100) / 100,
        y: k.y - Math.floor(s * p * Math.cos(f * Math.PI / 180) * 100) / 100
    }
}

function direction(dt) {
    var val = dt;
    // if (val >= 337.5 && val < 22.5) {
    //   console.log("正北");
    //   val = 0;
    // } else if (val >= 22.5 && val < 67.5) {
    //   console.log("东北");
    //   val = 45;
    // } else if (val >= 67.5 && val <= 112.5) {
    //   console.log("正东");
    //   val = 90;
    // } else if (val >= 112.5 && val < 157.5) {
    //   console.log("东南");
    //   val = 135;
    // } else if (val >= 157.5 && val <= 202.5) {
    //   console.log("正南");
    //   val = 180;
    // } else if (val >= 202.5 && val < 247.5) {
    //   console.log("西南");
    //   val = 225;
    // } else if (val >= 247.5 && val < 292.5) {
    //   console.log("正西");
    //   val = 270;
    // } else if (val >= 292.5 && val < 337.5) {
    //   console.log("西北");
    //   val = 315;
    // }
    return Math.round(val);
}

var sma = new Array();

function queue(res) { //加速度计原始数据
    var smaData = {};
    smaData.x = 0;
    smaData.y = 0;
    smaData.z = 0;
    if (sma.length > 4) {
        sma.unshift(res);
        sma.pop();
        for (let e = 0; e < sma.length; e++) {
            smaData.x += sma[e].x * 9.8;
            smaData.y += sma[e].y * 9.8;
            smaData.z += sma[e].z * 9.8;
        }
        smaData.x = smaData.x / sma.length.toFixed(5);
        smaData.y = smaData.y / sma.length.toFixed(5);
        smaData.z = smaData.z / sma.length.toFixed(5);

        return smaData
    } else {
        sma.unshift(res);
        return ''
    };
}

module.exports = {
    pointInPoly: pointInPoly,
    point_status: point_status,
    searchMax: searchMax,
    pauxi: pauxi,
    formats: formats,
    format: format,
    coordinate: coordinate,
    direction: direction,
    queue: queue,
    getAngle: getAngle,
}